package com.itshen.service.impl;

import com.itshen.entity.City;
import com.itshen.repository.CityRepository;
import com.itshen.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 城市 ES 业务逻辑实现类
 *
 * @author stephen
 */
@Slf4j
@Service
public class CityServiceImpl implements CityService {

    @Autowired
    CityRepository cityRepository;

    @Override
    public Long saveCity(City city) {
        City cityResult = cityRepository.save(city);
        return cityResult.getId();
    }

    @Override
    public City findCityById(Long id) {
        return cityRepository.findCityById(id);
    }


    @Override
    public List<City> searchCity(Integer pageNumber,
                                 Integer pageSize,
                                 String searchContent) {

        //按描述进行搜索
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("description", searchContent);
        // 构造分页
        Pageable pageable = PageRequest.of(pageNumber,pageSize) ;
        Page<City> searchPageResults = cityRepository.search(queryBuilder,pageable);
        return searchPageResults.getContent();
    }

}
